import {
  Controller,
  Get,
  // Post,
  Body,
  Patch,
  Param,
  Delete,
  UseInterceptors,
  ClassSerializerInterceptor,
} from '@nestjs/common';
import { UserService } from './user.service';
// import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
// import { SkipAuth } from 'src/auth/decorator/skip-auth.decorator';
import { User } from 'src/auth/decorator/user.decorator';
import { UserEntity } from './entities/user.entity';
import { BusinessException } from 'src/common/filter/business-exception/business.exception';

@ApiTags('用户')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  // @ApiOperation({ summary: '添加用户' })
  // @Post()
  // @SkipAuth()
  // @UseInterceptors(ClassSerializerInterceptor)
  // create(@Body() createUserDto: CreateUserDto) {
  //   return this.userService.create(createUserDto);
  // }

  @Get()
  @UseInterceptors(ClassSerializerInterceptor)
  findAll() {
    return this.userService.findAll();
  }

  @Get('info')
  @UseInterceptors(ClassSerializerInterceptor)
  findOne(@User() user: UserEntity) {
    return this.userService.findOne(user.id);
  }

  @ApiOperation({ summary: '修改昵称' })
  @Patch('info')
  updateInfo(
    @Param('id') id: string,
    @Body() updateUserDto: UpdateUserDto,
    @User() user: UserEntity,
  ) {
    // TODO 昵称校验
    if (!updateUserDto.name) throw new BusinessException('请输入昵称');
    return this.userService.updateInfo(id, updateUserDto, user);
  }

  @Patch('password')
  updatePassword(
    @Param('id') id: string,
    @Body() updateUserDto: UpdateUserDto,
    @User() user: UserEntity,
  ) {
    // TODO 校验密码
    if (!updateUserDto.check_password || !updateUserDto.password)
      throw new BusinessException('密码参数缺失');
    return this.userService.updatePassword(id, updateUserDto, user);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }
}
